package org.elsfs.framework.lang;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * in.
 *
 * @author zeng
 * @since 0.0.1
 */
public class ClassUtils {

  private static final Logger log = LoggerFactory.getLogger(ClassUtils.class);

  private static final HashMap<String, Class<?>> PRIM_CLASSES = new HashMap<>(8, 1.0F);

  static {
    PRIM_CLASSES.put("boolean", boolean.class);
    PRIM_CLASSES.put("byte", byte.class);
    PRIM_CLASSES.put("char", char.class);
    PRIM_CLASSES.put("short", short.class);
    PRIM_CLASSES.put("int", int.class);
    PRIM_CLASSES.put("long", long.class);
    PRIM_CLASSES.put("float", float.class);
    PRIM_CLASSES.put("double", double.class);
    PRIM_CLASSES.put("void", void.class);
  }

  /**
   * 获取带注释的方法
   *
   * @param type 类型
   * @param annotation 注解
   * @return 方法列表
   */
  public static List<Method> getAnnotatedMethods(
      final Class<?> type, final Class<? extends Annotation> annotation) {
    final List<Method> methods = new ArrayList<Method>();
    Class<?> clazz = type;
    while (!Object.class.equals(clazz)) {
      Method[] currentClassMethods = clazz.getDeclaredMethods();
      for (final Method method : currentClassMethods) {
        if (annotation == null || method.isAnnotationPresent(annotation)) {
          methods.add(method);
        }
      }
      // move to the upper class in the hierarchy in search for more methods
      clazz = clazz.getSuperclass();
    }
    return methods;
  }
}
